package com.szrt.base;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * xml解析类
 * @author My
 *
 */
public class XmlUtil {
	
//	static String  filePath="src/main/resource/generateEntityFromTable.xml";
//	static String  filePath="";
	
	/**
	 * 读取xml 
	 * @return  得到document对象
	 * @throws DocumentException
	 */
	public static Document getdocument(String filePath) throws DocumentException{
		
		SAXReader reader = new SAXReader();
		File file= new File(filePath);
		Document document = reader.read(file);
		return document;
	}
	
	/**
	 * 读xml文件返回tableName 
	 * @return  返回tableNamelist
	 * @throws DocumentException
	 */
	public static List<String> getTableName(Document document) throws DocumentException{
		
		//tableNameList表的名字
		List<String> tableNameList=new ArrayList<String>();

		@SuppressWarnings("unchecked")
		List<Node> selectNodes = document.selectNodes("//package/tableName");
		for (Node node : selectNodes) {
			
//			tmall_product_type_t  
			tableNameList.add(node.getStringValue());
		}
		return tableNameList;
	}
	
	/**
	 * 读xml文件返回tableName 
	 * @return  返回tableName
	 * @throws DocumentException
	 */
	public static String getTableName2(Document document) throws DocumentException{
		
		//tableName表的名字
		String tableName="";
		
		//不加//，得到的eles是一个空的list。如果是根节点可以不加//，这里在w3school有说明的
		//使用使用dom4j解析XML时，要快速获取某个节点的数据，使用XPath是个不错的方法，要加入<jaxen-1.1-beta-6.jar>
		Node selectSingleNode = document.selectSingleNode("//tableName");
		tableName = selectSingleNode.getText();
		
		return tableName;
	}
	
	/**
	 * 读xml文件返回package
	 * @return  返回packageName
	 * @throws DocumentException
	 */
	public static String getPackageName2(Document document) throws DocumentException{
			
		//tableNameList表的名字
			String packageName="";
			Node selectSingleNode = document.selectSingleNode("//package");
			packageName = selectSingleNode.getText();
			return packageName;
		}
	
	/**
	 * 读xml文件返回package
	 * @return  返回packageName
	 * @throws DocumentException
	 */
	public static String getPackageName(Document document) throws DocumentException{
		
		//tableNameList表的名字
		String packageName="";
		Node s = document.selectSingleNode("//package/@name");
		packageName = s.getText();
		return packageName;
	}
	
	

}
